﻿
#region References

using System;
using System.Net.Mail;
using App.Core.Logging;
using App.Core.Extensions;
using System.Net;


#endregion

namespace App.Core.Mail
{
    /// <summary>
    /// Reads messages from XML file
    /// </summary>
    public sealed class MailManager
    {
        #region SMTP Information
        public static String SmtpHost
        {
            get;
            set;
        }
        public static int SmtpPort
        {
            get;
            set;
        }
        #endregion SMTP Information

        #region Methods
        public static bool SendMail(string mailTo, string mailCc, string mailBcc, string mailFrom, string mailSubject, string mailBody)
        {
            try
            {
                using (MailMessage mailMessage = new MailMessage())
                {
                    mailMessage.From = new MailAddress(mailFrom);

                    //Spliting the to addresses by ','
                    string[] emailAddesses = mailTo.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string to in emailAddesses)
                    {
                        mailMessage.To.Add(new MailAddress(to.Trim()));
                    }
                    if (!mailBcc.IsNullOrEmpty())
                    {
                        //Spliting the cc Adresses by ','
                        string[] ccAddresses = mailCc.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        foreach (string cc in ccAddresses)
                        {
                            mailMessage.CC.Add(new MailAddress(cc.Trim()));
                        }
                    }

                    if (!mailBcc.IsNullOrEmpty())
                    {
                        //determining the BCC of the mail.
                        string[] bccAddresses = mailBcc.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        foreach (string bcc in bccAddresses)
                        {
                            mailMessage.Bcc.Add(new MailAddress(bcc.Trim()));
                        }
                    }

                    mailMessage.Subject = mailSubject;
                    mailMessage.Body = mailBody;
                    mailMessage.IsBodyHtml = true;

                    //sending the mail.
                    SmtpClient smtpClient = new SmtpClient();
                    smtpClient.Host = SmtpHost; //smtpHost;
                    smtpClient.Port = SmtpPort; //smtpPort;
                    //smtpClient.UseDefaultCredentials = true;
                    var userName = System.Configuration.ConfigurationManager.AppSettings["SmtpFromEmail"];                    
                    String password = System.Configuration.ConfigurationManager.AppSettings["SmtpFromEmailPassword"];
                    NetworkCredential credential = new NetworkCredential(userName, password);
                    smtpClient.Credentials = credential;
                    smtpClient.Send(mailMessage);
                    return true;
                }
            }
            catch (Exception ex)
            {
                //AppLogger.LogInfo("Unable to Send Email. For more details see the next error.");
                //AppLogger.LogException(ex);
                throw ex;
            }
            //return false;
        }

        public static bool SendEmailThroughGmail(String fromEmail, String toEmail, String subject, String body)
        {
            var fromAddress = new MailAddress(System.Configuration.ConfigurationManager.AppSettings["SmtpFromEmail"]
                                            , System.Configuration.ConfigurationManager.AppSettings["SmtpFromEmailName"]);
            var toAddress = new MailAddress(toEmail, String.Empty);
            String fromPassword = System.Configuration.ConfigurationManager.AppSettings["SmtpFromEmailPassword"];
            //String subject = "Subject";
            //const string body = "Body";

            SmtpClient smtp = new SmtpClient
            {
                Host = SmtpHost, //"smtp.gmail.com",
                Port = SmtpPort, //587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
            };
            using (var message = new MailMessage(fromAddress, toAddress)
            {
                IsBodyHtml = true,
                Subject = subject,
                Body = body
            })
            {
                smtp.Send(message);
            }
            return true;
        }
        #endregion
    }
}
